package com.otachn.servlet;

import java.io.ByteArrayInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.otachn.jdbc.DBLink;
import com.otachn.utiils.HeadPictureEntity;
import com.otachn.utiils.ImageUtil;
import com.otachn.utiils.InformationEntity;

/**
 * Servlet implementation class ServletQueryCertificate
 * @author ching
 */
//@WebServlet(description = "QueryCertificate", urlPatterns = { "/ServletQueryCertificate" })
public class ServletQueryCertificate extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    public ServletQueryCertificate() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("application/json; charset=utf-8");
		String shenfenzhenghao = request.getParameter("shenfenzhenghao");
		String zhengshubianhao = request.getParameter("zhengshubianhao");
		String sql = "select * from information where sfzh='" + shenfenzhenghao + "' and zsbh='" + zhengshubianhao + "';";
		InformationEntity ie = DBLink.getInformationEntity(sql);
		if(ie.getId() == 0) {
			request.getRequestDispatcher("result.html").forward(request, response);
		}
		else {
			String path = request.getSession().getServletContext().getRealPath("/");
			FileInputStream fis = ImageUtil.getImageByte(path + "/headpic/" + ie.getSfzh() + ".jpg");
			if (fis == null) {
				String sql1 = "select * from headpicture where sfzh='" + ie.getSfzh() + "';";
				HeadPictureEntity hpe = DBLink.getHeadPictureEntity(sql1);
				
				InputStream is = new ByteArrayInputStream(hpe.getZp());
				ImageUtil.readBlob(is, path + "/headpic/" + hpe.getSfzh() + ".jpg");
			}
			
			request.setAttribute("xm", ie.getXm());
			request.setAttribute("xb", ie.getXb());
			request.setAttribute("sfzh", ie.getSfzh());
			request.setAttribute("csrq", ie.getCsrq());
			request.setAttribute("zsbh", ie.getZsbh());
			request.setAttribute("zy", ie.getZy());
			request.setAttribute("bzrq", ie.getBzrq());
			request.setAttribute("llcj", ie.getLlcj());
			request.setAttribute("czcj", ie.getCzcj());
			request.setAttribute("pdcj", ie.getPdcj());
			request.setAttribute("pxdw", ie.getPxdw());
			request.setAttribute("fzdw", ie.getFzdw());
			request.setAttribute("zp", "./headpic/" + ie.getSfzh() + ".jpg");
			request.getRequestDispatcher("result.jsp").forward(request, response);
		}
	}

}
